<?php

namespace App\Http\Controllers\Web\V1;


use App\Models\Question;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Support\Facades\DB;
use App\Http\Controllers\Controller;
use App\Http\Requests\Web\SelfExerciseRequest;

class SelfExerciseController extends Controller
{
    /**
     *
     * 创建自主练习所需的数据
     *
     * Display a listing of the resource.
     *
     * @param Request $request
     * @return Response
     */
    public function index(Request $request)
    {
        $category = $request->input('category_id') ?? '';

        // 获取题目分类
        $data = cacheTextBank();

        // 获取所选分类下的题目
        $question = Question::query()
            ->when($category, function ($query) use($category){
                $query->where('category_id', $category);
            })
            ->where('status', 1)
            ->whereIn('type', ['radio', 'checkbox', 'judge'])
            ->selectRaw("type,count(type) AS num")
            ->groupBy('type')
            ->get();

        return $this->response->array(['category' => $data, 'question' => $question]);
    }


    /**
     *
     * 创建自主练习预览
     *
     * Display the specified resource.
     *
     * @param SelfExerciseRequest $request
     * @return Response
     */
    public function show(SelfExerciseRequest $request)
    {
        $radio = $request->input('radio');
        $checkbox = $request->input('checkbox');
        $judge = $request->input('judge');

        if ($radio == 0 && $checkbox == 0 && $judge == 0) {
            return $this->response->error('您没有选择任何题目', 422);
        }

        $category = $request->input('category_id') ? "`category_id` = {$request->input('category_id')} AND" : '';

        $sql = [];

        $order = ['id', 'created_at', 'order', 'category_id', 'title', 'content', 'fraction', 'level', 'num', 'success_num'][rand(0, 9)];

        $sql[] = "(select id,type,title,content,answer,success_answer,fraction from questions where {$category} `status` = 1  AND `type` = 'radio' order by `" . $order . "` " . ['desc', 'asc'][rand(0, 1)] . " limit {$request->input('radio')})";
        $sql[] = "(select id,type,title,content,answer,success_answer,fraction from questions where {$category} `status` = 1  AND `type` = 'checkbox' order by `" . $order . "` " . ['desc', 'asc'][rand(0, 1)] . " limit {$request->input('checkbox')})";
        $sql[] = "(select id,type,title,content,answer,success_answer,fraction from questions where {$category} `status` = 1  AND `type` = 'judge' order by `" . $order . "` " . ['desc', 'asc'][rand(0, 1)] . " limit {$request->input('judge')})";

        // 合并sql语句
        $sql = implode(' union ', $sql);

        $data = DB::select($sql);

        $questionData = [];
        $fraction = 0;

        foreach ($data as $key => $item) {
            $questionData[$item->type][] = $data[$key];
            $fraction += $item->fraction;
        }

        return $this->response->array(['data' => $questionData, 'timer' => $request->timer, 'fraction' => $fraction]);
    }

    /**
     * 自主练习单题批改
     *
     * @param Request $request
     * @return \Dingo\Api\Http\Response
     */
    public function correct(Request $request)
    {
        $request->validate([
            'question_id' => 'required',
            'answer' => 'required',
            'fraction' => 'required|integer'
        ]);

        $data = autoCorrect($request->all());

        return $this->response->array($data);
    }
}
